package xml.document;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import base.check.Check;

public class DocumentHelper {
	
	public static void check(Element root, String name)  {
		Check.isTrue(name.equals(root.getName()), Check.raiseException, "Unknown element " + root.getName());
	}
	
	
	public static Document read(InputStream inputStream) {
		SAXBuilder builder = new SAXBuilder();
		
		try {
			return builder.build(inputStream);
		} catch (JDOMException e) {
			
		} catch (IOException e) {
		}
		return null;
	}
	
	public static void write(Document doc, OutputStream outStream) throws FileNotFoundException, IOException {
		XMLOutputter outputter = new XMLOutputter();
		outputter.setFormat(Format.getPrettyFormat());
		outputter.output(doc, outStream);
	}
}
